﻿<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<title>丫丫豆-都市信息平台</title>
		<link href="/city_info/favicon.ico" rel="shortcut icon" type="image/x-icon" />
		<link rel="stylesheet" href="${pageContext.request.contextPath}/css/reset.css"/>
		<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.11.0.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-migrate-1.2.1.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath}/js/commons.js"></script>
		<style>
			#wrapper{ width:98%; border:#cbcbcb 1px solid; padding-top:17px; padding-left:14px; background:#f4f1e8;}
			#header{ padding-bottom:18px;}
			.box{ width:99%; border:#cbcbcb 1px solid; background:#ffffff;}
			.tab{ color:#c32c33; margin:0 auto;}
			.tab td{ text-align:center; height:40px; padding:0 10px;}
			.tab th{ background:url('${pageContext.request.contextPath}/images/tabThBg.png') right center no-repeat; padding:13px 10px;}
			.field{ cursor:pointer;}
			#header .search{ float:left;}
			.select{ float:left; vertical-align:middle;}
			.select select{ height:32px; line-height:32px; vertical-align:middle;}
		</style>
		<script>
			
		</script>
	</head>
<body>

<div id="wrapper">
	<div id="header" class="clear">
        <div class="search">
	        <form action="" method="post" id="searchForm">
	        	<input type="hidden" name="_method" value="selectNotice"/>
	            <input type="text" value="" id="title" name="title" class="text">
	            <input type="submit" value="" class="submit">
	        </form>
        </div>
         <div class="export">
        	<input type="button" value="发布" id="publishBtn" class="selectBtn">
        	<input type="button" value="删除" id="delByCheck" class="selectBtn">
        	<span style="color:red">${tip}</span>
        </div>
    </div>
    
    <div class="box">
    	<table width="98%" class="tab">
        	<tr style="border-bottom:#c32c33 1px solid;">
            	<th><input type="checkbox" id="checkAll" class="checked"></th>
            	<th>信息类型</th>
            	<th>编号</th>
                <th>标题</th>
                <th>创建时间</th>
                <th>发布时间</th>
                <th>发布状态</th>
                <th>发布人</th>
                <th style="background:none;" colspan="2">操作</th>
            </tr>
            <!-- 迭代数据 -->
            	<c:forEach items="${notices}" var="notice" varStatus="ids">
	           		 <tr class="field" style="border-bottom:#f3d5d6 1px dashed;">
		            	<td><input class="checkbox" value="6" id="box_${notice.id}" type="checkbox"/> </td>
		            	<td>【${notice.type.name}】</td>
		            	<td>${ids.index+1}</td>
		                <td>${notice.title}</td>
		                <td>
		                	<fmt:formatDate value="${notice.createDate}" pattern="yyyy/MM/dd"/>
		                </td>
		                <td>${notice.publishDate}</td>
		                <td>
		                	<c:choose>
		                		<c:when test="${notice.publishStatus==1}">
		                			<font color=red>已发布</font>
		                			<input type="hidden" id="publish_${notice.id}" value="1"/>
		                		</c:when>
		                		<c:otherwise>
		                			<font color="blue">未发布</font>
		                			<input type="hidden" id="publish_${notice.id}" value="0"/>
		                		</c:otherwise>
		                	</c:choose>
		                	
		                	
		                	
		                </td>
		                <td>${notice.user.name}</td>
								<td><a href="javascript:void(0);" onclick="deleteFn(${notice.id});" style="color:blue;">删除</a></td>
								 <td><a href="javascript:void(0);" onclick="updateFn(${notice.id});" style="color:blue;">修改</a></td>
						</tr>
	           </c:forEach>
          	
	           
          	
        </table>
    </div>
    <!-- 判断总页数不等于1时，显示分页信息 -->
    
	    <div align="center">
	    	<!-- 控制首页 与 上一页  -->
	    	<c:if test="${pageModel.pageIndex==0}">
	    		<a>首页</a>
	    		<a>上一页</a>
	    	</c:if>
	    	<!-- 当前页面大于0 -->
	    	<c:if test="${pageModel.pageIndex>0}">
	    		【<a href="${pageContext.request.contextPath}/cityController?_method=notices&pageIndex=0">首页</a>】
	    		【<a href="${pageContext.request.contextPath}/cityController?_method=notices&pageIndex=${pageModel.pageIndex-1}">上一页</a>】
	    	</c:if>
	    	
	    	<!-- 当前页面<最后页 -->
	    	<c:if test="${pageModel.pageIndex<(pageModel.count-1)}">
	    		【<a href="${pageContext.request.contextPath}/cityController?_method=notices&pageIndex=${pageModel.pageIndex+1}">下一页</a>】
	    		【<a href="${pageContext.request.contextPath}/cityController?_method=notices&pageIndex=${pageModel.count-1}">尾页</a>】
	    	</c:if>
	    	
	    	<!-- 控制下一页 与 尾页-->
	    	<c:if test="${pageModel.pageIndex==(pageModel.count-1)}">
	    		<a>下一页</a>
	    		<a>尾页</a>
	    	</c:if>
	    	
	    	总页数:${pageModel.count}
	   </div>
    
</div>
</body>
<script type="text/javascript">
	//根据id删除对应记录
	function  deleteFn(id){
		//alert("删除记录:"+id);
		
		if(confirm("您确定要删除该记录?")){
			location.href="${pageContext.request.contextPath}/cityController?_method=delNoticeById&id="+id;
		}
	}
	
	//根据id修改对应记录
	function updateFn(id){
		location.href="${pageContext.request.contextPath}/cityController?_method=updateNoticeById&id="+id;
	}
	
	
	$(function(){
		//全选
		$("#checkAll").click(function(){
			$("input[id^=box_]").attr("checked",this.checked);
		})
		
		//批量删除
		$("#delByCheck").click(function(){
			var boxs = $("input[id^=box_]:checked");
			//定义数组
			var idArray = new Array();
			
			if(boxs.length>0){//用户勾选了记录
				boxs.each(function(i){
					idArray[i]=this.id.replace("box_","");
				})
				//发送请求
				location.href="${pageContext.request.contextPath}/cityController?_method=delBatchByIds&ids="+idArray;
			}else{
				alert("请勾选至少一条记录!");
			}
		})
		
		
		//批量发布信息
		$("#publishBtn").click(function(){
			var boxs = $("input[id^=box_]:checked");
			//定义数组
			var ids = new Array();
			var flag = true;
			boxs.each(function(index){
				//判断是否有已发布数据
				
				//把id存到数组中
				var id = this.id.replace("box_","");
				ids[index]=id;
				var status = $(this.id.replace("box_","#publish_")).val();
				if(status>0){
					flag=false;
					return ;
				}
			})
			console.log("要发布的id:"+ids);
			if(flag){//true:则发送发布请求，false:提示用户
				
				window.location.href="${pageContext.request.contextPath}/cityController?_method=publishNoticeByIds&ids="+ids;
			}else{
				alert("请选择未发布的记录!");
			}
		})
		
	})

</script>
</html>